package com.unknow.cpt;
/**
 * FacebookActivity manages the facebook login and logout functions.
 * FacebookActivity needs to call com.facebook.android to remain the full functionalities.
 * 
 * Usage:
 * 		When resume this activity: 
 * 		It checks the status of facebook login by looking into the SharedPreferences
 * 			If there is already a user login, a logout question dialog will be pop up. 
 * 			Click Yes to logout, or No nothing happends.
 * 			If there is no user login yet, it calls facebook login function.
 * 		All login information includes facebookID, facebookName, ACCESS_TOKEN, ACCESS_EXPIRES
 * 		will be store in the SharedPreferences.
 * @author Mali
 * 
 */

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Util;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class FacebookActivity extends Activity{
	private Facebook fb;
	private SharedPreferences sp;
	private Editor editor;
	private AlertDialog.Builder builder;
	public static FacebookActivity instances;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setTheme(R.style.transparent_activity);
		sp = MainActivity.sp;
		editor = sp.edit();
		fb = MainActivity.fb;
		instances = this;
		//Check login status
		if(fb.isSessionValid()){
			//Should logout;
			Log.i("debug", "should logout");
			String access_token = sp.getString("ACCESS_TOKEN", null);
			long access_expires = sp.getLong("ACCESS_EXPIRES", 0);
			if(access_token!=null){
				fb.setAccessToken(access_token);
			}
			if(access_expires!=0){
				fb.setAccessExpires(access_expires);
			}
			//Show logout dialog;
			builder = new Builder(FacebookActivity.this);
			builder.setMessage("Do you want to logout?");
			builder.setTitle(sp.getString("FACEBOOK_USER", ""));
			builder.setPositiveButton("YES", new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// Call facebook logout funcion
					Runnable r = new Runnable(){  
						@Override  
						public void run() {
							try {
								fb.logout(getApplicationContext());
							} catch (MalformedURLException e) {
								// TODO Auto-generated catch block
								Toast.makeText(getApplicationContext(),"Logout failed", Toast.LENGTH_SHORT).show();
								e.printStackTrace();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								Toast.makeText(getApplicationContext(),"Logout failed", Toast.LENGTH_SHORT).show();
								e.printStackTrace();
							}
						}  
					
					};
					new Thread(r).start();
					finish();
				}
				
			});
			builder.setNegativeButton("NO", new DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// Nothing happends;
					finish();
				}
				
			});
			builder.show();
		}else{
			login();
		}
	}
	/**
	 * Facebook login function.
	 */
	public void login(){
		fb.authorize(this, new DialogListener(){

			@Override
			public void onComplete(Bundle values) {
				// Login Successful
				Toast.makeText(getApplicationContext(),"Login successful", Toast.LENGTH_SHORT).show();
				editor.putString("ACCESS_TOKEN", fb.getAccessToken());
				editor.putLong("ACCESS_EXPIRES", fb.getAccessExpires());
				editor.commit();
				Runnable r = new Runnable(){  
					@Override  
					public void run() {
						HashMap<String, String> userInfo = getUserInfo();
						editor.putString("FACEBOOK_USER", userInfo.get("user_name"));
						editor.putString("FACEBOOK_ID", userInfo.get("user_id"));
						editor.commit();
					}  
				
				};
				new Thread(r).start();
				editor.commit();
				finish();
			}

			@Override
			public void onFacebookError(FacebookError e) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(),"Error occured when login", Toast.LENGTH_SHORT).show();
				finish();
			}

			@Override
			public void onError(DialogError e) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(),"Error occured when login", Toast.LENGTH_SHORT).show();
				finish();
			}

			@Override
			public void onCancel() {
				// User cancel operation, nothing happends.
				finish();
			}
			
		});
	}
	/**
	 * Get facebook user information include user_name and user_id;
	 * @return HashMap<String,String>
	 */
	private HashMap<String,String> getUserInfo(){
		JSONObject obj = null;
		String jsonUser;
		try {
			jsonUser = fb.request("me");
			obj = Util.parseJson(jsonUser);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FacebookError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap<String,String> hm = new HashMap<String,String>();
		hm.put("user_name", obj.optString("name"));
		hm.put("user_id", obj.optString("id"));
		return hm;
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		finish();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}
}
